package com.example.bicycle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bicycle.domain.dto.SearchRecordsDto;
import com.example.bicycle.domain.entity.RideRecord;
import com.example.bicycle.domain.vo.PageVo;
import com.example.bicycle.mapper.RideRecordMapper;
import com.example.bicycle.service.RideRecordService;
import com.example.bicycle.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * (RideRecord)表服务实现类
 *
 * @author makejava
 * @since 2023-08-02 21:29:49
 */
@Service("rideRecordService")
public class RideRecordServiceImpl extends ServiceImpl<RideRecordMapper, RideRecord> implements RideRecordService {
    private final UserService userService;

    @Autowired
    public RideRecordServiceImpl(UserService userService) {
        this.userService = userService;
    }

    @Override
    public RideRecord getLatestRecordByBikeId(int bikeId) {
        /* 先根据startTime时间排序然后取最新的 */
        QueryWrapper<RideRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("startTime");
        queryWrapper.eq("bikeId", bikeId);
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    /**
     * 获取所有记录
     *
     * @param pageNum  页面num
     * @param pageSize 页面大小
     * @return {@link PageVo}
     */
    @Override
    public PageVo getAllRecords(int pageNum, int pageSize) {
        QueryWrapper <RideRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("startTime");
        Page page = new Page(pageNum, pageSize);
        page(page,queryWrapper);
        return new PageVo(page.getRecords(),page.getTotal());
    }

    /**
     * 被用户id记录
     *
     * @param pageNum  页面num
     * @param pageSize 页面大小
     * @param userId   用户id
     * @return {@link PageVo}
     */
    @Override
    public PageVo getRecordsByUserId(int pageNum, int pageSize, int userId) {
        QueryWrapper <RideRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("startTime");
        queryWrapper.eq("userId", userId);
        Page page = new Page(pageNum, pageSize);
        page(page,queryWrapper);
        return new PageVo(page.getRecords(),page.getTotal());
    }

    /**
     * 骑自行车会记录id
     *
     * @param pageNum  页面num
     * @param pageSize 页面大小
     * @param bikeId   自行车id
     * @return {@link PageVo}
     */
    @Override
    public PageVo getRecordsByBikeId(int pageNum, int pageSize, int bikeId) {
        QueryWrapper <RideRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("startTime");
        queryWrapper.eq("bikeId", bikeId);
        Page page = new Page(pageNum, pageSize);
        page(page,queryWrapper);
        return new PageVo(page.getRecords(),page.getTotal());
    }

    /**
     * 被用户id和自行车记录id
     *
     * @param pageNum  页面num
     * @param pageSize 页面大小
     * @param userId   用户id
     * @param bikeId   自行车id
     * @return {@link PageVo}
     */
    @Override
    public PageVo getRecordsByUserIdAndBikeId(int pageNum, int pageSize, int userId, int bikeId) {
        QueryWrapper <RideRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("startTime");
        queryWrapper.eq("userId", userId);
        queryWrapper.eq("bikeId", bikeId);
        Page page = new Page(pageNum, pageSize);
        page(page,queryWrapper);
        return new PageVo(page.getRecords(),page.getTotal());
    }

    @Override
    public RideRecord getLatestRecordByUserId(Integer userId) {
        QueryWrapper <RideRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("startTime");
        queryWrapper.eq("userId", userId);
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @Override
    public PageVo getRecordsByUserIdOrBikeId(int pageNum, int pageSize, SearchRecordsDto searchRecordsDto) {

        QueryWrapper <RideRecord> queryWrapper = new QueryWrapper<>();
        if(!searchRecordsDto.getUserId().equals("")){
            queryWrapper.eq("userId", Integer.valueOf(searchRecordsDto.getUserId()));
        }
        if(!searchRecordsDto.getBikeId().equals("")){
            queryWrapper.eq("bikeId", Integer.valueOf(searchRecordsDto.getBikeId()));
        }
        Page page = new Page(pageNum, pageSize);
        page(page,queryWrapper);
        return new PageVo(page.getRecords(),page.getTotal());
    }
}

